/*
 *  portable.c
 *  
 *
 *  Created by Michael Curtis on 20/09/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "config.h"

#include <stdlib.h>

#include "portable.h"

#ifndef HAVE_STRNLEN
size_t strnlen(const char *s, size_t n)
{
	size_t length;
	
	for( length = 0; length < n; length++ )
	{
		if( s[length] == '\0' )
		{
			break;
		}
	}
	
	return length;
}
#endif

#ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n)
{
	size_t length = strnlen( s, n );
	char *dup;
	
	if ( length < n ) length++;
	
	dup = malloc( length );
	if( dup == NULL )
	{
		return NULL;
	}
	
	strncpy( dup, s, length );
	dup[length ] = '\0';

	return dup;
}
#endif
